package com.kcl.dfss;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import com.kcl.dfss.http.CameraConnection;
import com.kcl.dfss.http.CameraConnection.ResponseHandler;
import android.annotation.SuppressLint;
import android.app.ListActivity;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.util.Log;
import android.view.Window;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ArrayAdapter;

public class SaveRecord extends ListActivity {
	private List<String> mRecordList = new ArrayList<String>();
	private String tag = "SaveRecord";
	private Handler handler = null;

	@SuppressLint("HandlerLeak")
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);

		// Use an existing ListAdapter that will map an array
		// of strings to TextViews
		requestWindowFeature(Window.FEATURE_NO_TITLE);
		getRecordsFromCamera();
		Log.i(tag , "list中的数据1："+mRecordList.toString());
		handler = new Handler(){

			@Override
			public void handleMessage(Message msg) {
				if(msg.what == 1){
					setListAdapter(new ArrayAdapter<String>(SaveRecord.this,
							android.R.layout.simple_list_item_1, mRecordList));
				}
			}
			
		};
		/*setListAdapter(new ArrayAdapter<String>(this,
				android.R.layout.simple_list_item_1, mRecordList));*/
		/*getListView().setTextFilterEnabled(true);
		
		getListView().setOnItemClickListener(new OnItemClickListener() {

			@Override
			public void onItemClick(AdapterView<?> parent, View view,
					int position, long id) {
				// TODO Auto-generated method stub
				String url = mRecordList.get(position);
				Intent intent = new Intent();
				intent.setClass(getBaseContext(), VideoPlayerActivity.class);
				intent.putExtra("URL", url);
				startActivity(intent);
			}
		});*/
	}

	public void getRecordsFromCamera() {
		if (LocationService.getCamemraConnected()) {
			CameraConnection mConnection = new CameraConnection(
					new ResponseHandler() {
						@Override
						public void handleResponse(int request_code,
								String response) {
							// TODO Auto-generated method stub
							if (request_code == CameraConnection.CODE_GET_FILELIST) {
								Log.i(tag , "response中的数据："+response);
								getRecordsFromResponse(response);
								Log.i(tag , "list中的数据2："+mRecordList.toString());
								Message message = new Message();
								message.what = 1;
								handler.sendMessage(message);
							}
						}
					}, this.getBaseContext());
			
			mConnection.getFilelist();
		}
	}

	public void getRecordsFromResponse(String string) {
		String[] list = string.split("\n");
		mRecordList.addAll(Arrays.asList(list));
	}
}
